package com.lzm.ydpt.module.order.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.basebean.ListPageBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.UserOrderBean;
import com.lzm.ydpt.module.order.activity.UserOrderDetailActivity;
import com.lzm.ydpt.mvp.contract.UserOrderListContract;
import com.lzm.ydpt.mvp.presenterimpl.UserOrderListPresenterImpl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 订单列表
 */
public class UserOrderListFragment extends BaseFragment<UserOrderListPresenterImpl> implements UserOrderListContract.View {

    @BindView(R.id.ltp_order_list)
    LoadingTip ltp_riding_order_list;
    @BindView(R.id.srf_order_list)
    SmartRefreshLayout srf_order_list;
    @BindView(R.id.rv_order_list)
    RecyclerView rv_order_list;

    private static UserOrderListFragment sFragment = null;
    private int type = 0;
    private int pageNum = 1;
    private RvCommonAdapter<UserOrderBean> adapter = null;
    private List<UserOrderBean> mList = new ArrayList<>();

    /**
     * 0全部 1待付款 2待接单 3待取货 4进行中(送货中) 5退款中 6已取消 7已完成 ,
     *
     * @param type
     * @return
     */
    public static UserOrderListFragment newsInstance(int type) {
        sFragment = new UserOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.INP_ORDER_TYPE, type);
        sFragment.setArguments(bundle);
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_order_list;
    }

    @Override
    public UserOrderListPresenterImpl initPresenter() {
        return new UserOrderListPresenterImpl(this);
    }

    @Override
    protected void initView() {
        type = getArguments().getInt(Constants.INP_ORDER_TYPE, 0);
        initRv();
        initAdapter();
    }

    @Override
    public void initFristData() {
        super.initFristData();
        ltp_riding_order_list.setLoadingTip(LoadingTip.LoadStatus.loading);
        getUserOrderList();
    }

    private void getUserOrderList() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("memberId", Dapplication.getUserInfo().getMemberId());
        formBodys.add("status", type);
        formBodys.add("pageSize", 10);
        formBodys.add("pageNum", 1);
        mPresenter.onUserOrderList(formBodys.build());
    }

    private void initRv() {
        setRefreshLayout(srf_order_list);
        srf_order_list.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffF4());
        srf_order_list.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                getUserOrderList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                getUserOrderList();
            }
        });
    }

    private void initAdapter() {
        rv_order_list.setLayoutManager(new LinearLayoutManager(context));
        adapter = new RvCommonAdapter<UserOrderBean>(context, mList, R.layout.item_user_order_rv) {
            @Override
            protected void convert(RvViewHolder holder, UserOrderBean orderBean, int position) {
                holder.setText(R.id.tv_user_order_code, orderBean.getOrderNo());
                holder.setText(R.id.tv_order_mail_info, orderBean.getSendContacts() + "  " + orderBean.getSendPhone());
                holder.setText(R.id.tv_order_mail_address, orderBean.getSendAddress() + orderBean.getSendDetailedAddress());

                holder.setText(R.id.tv_order_delivery_info, orderBean.getReceiveContacts() + "  " + orderBean.getReceivePhone());
                holder.setText(R.id.tv_order_delivery_address, orderBean.getReceiveAddress() + orderBean.getReceiveDetailedAddress());
                holder.setText(R.id.tv_order_distance, "距离：" + orderBean.getPositionDistance() + "km");
                holder.setText(R.id.tv_order_amount, "金额：¥" + orderBean.getTipsPrice());
                if (orderBean.getStatus() == 1) {
                    holder.setText(R.id.tv_user_order_state, "待支付");
                    holder.setVisible(R.id.tv_remain_pay_time, true);
                    holder.setText(R.id.tv_remain_pay_time, "剩余支付时间：10分30秒");

                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "去支付");
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, false);
                    holder.setVisible(R.id.tv_pick_info, false);
                } else if (orderBean.getStatus() == 2) {
                    holder.setText(R.id.tv_user_order_state, "待接单");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, false);
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, false);
                    holder.setVisible(R.id.tv_pick_info, true);
                    holder.setText(R.id.tv_pick_info, "骑手取件时间：21:30-22:00");
                } else if (orderBean.getStatus() == 3) {
                    holder.setText(R.id.tv_user_order_state, "待取货");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, false);
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, false);
                    holder.setVisible(R.id.tv_pick_info, true);
                    holder.setText(R.id.tv_pick_info, "骑手:张小成13800138560");

                } else if (orderBean.getStatus() == 4) {
                    holder.setText(R.id.tv_user_order_state, "进行中");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "复制");
                    holder.setVisible(R.id.tv_receiving_time, true);
                    holder.setText(R.id.tv_receiving_time, "收货码：8641");
                    holder.setVisible(R.id.tv_service_type, false);
                    holder.setVisible(R.id.tv_pick_info, false);
                } else if (orderBean.getStatus() == 5) {
                    holder.setText(R.id.tv_user_order_state, "已完成");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "再来一单");
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, true);
                    holder.setText(R.id.tv_service_type, "送达时间：2020-07-21 22:00");
                    holder.setVisible(R.id.tv_pick_info, false);
                } else if (orderBean.getStatus() == 6) {
                    holder.setText(R.id.tv_user_order_state, "已取消");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "再来一单");
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, true);
                    holder.setText(R.id.tv_service_type, "取消时间：2020-07-21 22:00");
                    holder.setVisible(R.id.tv_pick_info, false);

                } else if (orderBean.getStatus() == 7) {
                    holder.setText(R.id.tv_user_order_state, "退款中");
                    holder.setVisible(R.id.tv_remain_pay_time, false);

                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "再来一单");
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, true);
                    holder.setText(R.id.tv_service_type, "正在原路退款中...");
                    holder.setVisible(R.id.tv_pick_info, false);

                } else if (orderBean.getStatus() == 8) {
                    holder.setText(R.id.tv_user_order_state, "已退款");
                    holder.setVisible(R.id.tv_remain_pay_time, false);
                    holder.setVisible(R.id.tv_order_use, true);
                    holder.setText(R.id.tv_order_use, "再来一单");
                    holder.setVisible(R.id.tv_receiving_time, false);
                    holder.setVisible(R.id.tv_service_type, true);
                    holder.setText(R.id.tv_service_type, "已原路退款15元");
                    holder.setVisible(R.id.tv_pick_info, false);
                }

                holder.setOnClickListener(R.id.tv_order_use, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if (orderBean.getStatus() == 1) {
                            //待支付 去支付
                        } else if (orderBean.getStatus() == 2) {
                            //待接单 没有操作
                        } else if (orderBean.getStatus() == 3) {
                            //待取货 没有操作
                        } else if (orderBean.getStatus() == 4) {
                            //进行中 复制收货码
                        } else if (orderBean.getStatus() == 5) {
                            //已完成 再来一单
                        } else if (orderBean.getStatus() == 6) {
                            //已取消 再来一单
                        } else if (orderBean.getStatus() == 7) {
                            //退款中 再来一单
                        } else if (orderBean.getStatus() == 8) {
                            //已退款 再来一单
                        }
                    }
                });

                holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        openActivity(UserOrderDetailActivity.class);
                    }
                });
            }
        };
        rv_order_list.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }

    @Override
    public void onUserOrderList(ListPageBean<UserOrderBean> orderList) {
        if (pageNum == 1) {
            mList.clear();
            srf_order_list.finishRefresh();
        } else {
            srf_order_list.finishLoadMore();
        }
        mList.addAll(orderList.getList());
        adapter.notifyDataSetChanged();
        srf_order_list.setEnableLoadMore(orderList.getList().size() >= 10);
        ltp_riding_order_list.setLoadingTip(mList.size() > 0 ? LoadingTip.LoadStatus.finish : LoadingTip.LoadStatus.empty);
    }

    @Override
    public void showMessage(String message, String method) {

    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltp_riding_order_list);
        ltp_riding_order_list.setOnReloadListener(new LoadingTip.onReloadListener() {
            @Override
            public void reload(View view) {
                ltp_riding_order_list.setLoadingTip(LoadingTip.LoadStatus.loading);
                getUserOrderList();
            }
        });
    }
}